package be.phl.obstacleapp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ItemProblemColumnAdapter extends BaseAdapter {

	private List<ItemProblem> datalist;
	private List<ItemProblemRowAdapter> lists = new ArrayList<ItemProblemRowAdapter>();
	private Activity activity;
	private int ItemId;
	
	public ItemProblemColumnAdapter(Activity activity, int itemId) {
		super();
		this.activity = activity;
		this.ItemId = itemId;
		
		datalist = new ArrayList<ItemProblem>();

		// array with testdata
		int tempCount = 0;
		for (int i = 0; i <= 5; i++) {
			int id1 = tempCount++;
			datalist.add(new ItemProblem(id1, 0, -1, "Problem" + i, false, true));
			for (int j = 0; j <= 5; j++) {
				int id2 = tempCount++;
				datalist.add(new ItemProblem(id2, 0, id1, "Problem" + i + j,
						false, true));
				for (int k = 0; k <= 5; k++) {
					int id3 = tempCount++;
					datalist.add(new ItemProblem(id3, 0, id2, "Problem" + i + j
							+ k, false, true));
					for (int l = 0; l <= 5; l++) {
						datalist.add(new ItemProblem(tempCount++, 0, id3,
								"Problem" + i + j + k + l, false, true));
					}
				}
			}
		}

		//get a list of all the parentId's
		List<Integer> tempParent = new ArrayList<Integer>();
		for(ItemProblem i : datalist) {
			if(!tempParent.contains(i.getParentId()) && i.getParentId() >= 0) {
				tempParent.add(i.getParentId());
			}
		}
		
		//check each item if it is another items parentId
		//if it is not, it is the and of a branch
		for(ItemProblem i: datalist) {
			if(!tempParent.contains(i.getId())) {
				i.setBranchEnd(true);
			}
		}
		
		fillTreeView(-1);
	}
	
	/**
	 * 
	 * Fill the content of the treeview
	 * add a row based on the id from the item clicked
	 * 
	 * @param ParentId
	 */
	public void fillTreeView(int problemId) {

		//temporary list to collect the data for the new column
		List<ItemProblem> tempList = new ArrayList<ItemProblem>();
		for (ItemProblem i : datalist) {
			if (i.getParentId() == problemId) {
				tempList.add(i);
			}
		}
		
		//add the temporary list to the adapter
		lists.add(new ItemProblemRowAdapter(this.activity, tempList, false));
	}

	public int getCount() {
        return lists.size();
    }  

    @Override  
    public Object getItem(int position) {  
        return lists.get(position);  
    }  

    @Override  
    public long getItemId(int position) {  
        return 0;  
    }

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
    	LayoutInflater inflater =  activity.getLayoutInflater();
		View columnView = inflater.inflate(R.layout.item_problem_column, parent,
				false);
		
		ListView listview = (ListView) columnView.findViewById(R.id.list);
		listview.setAdapter(lists.get(position)); 
		
		final int posX = position;
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int posY, long id) {
				// TODO Auto-generated method stub
				
				// if the clicked item is not in the last column, delete the column's after clicked column
				while(posX < (lists.size()-1)) {
					lists.remove(lists.size()-1);
				}
				ItemProblem problem = (ItemProblem) parent.getAdapter()
						.getItem(posY);
				
				// check if the item is at the end of a branck
				if(!problem.isBranchEnd()) {
					fillTreeView(problem.getId());
				}
				ItemProblemColumnAdapter.this.notifyDataSetChanged();
				
			}});
		
        return columnView;  
    } 

}
